import axios from 'axios';
import { IallLineInfo } from '../type/allLineInfoType';

// 获取1-9路公交线路信息集合
const allLineInfo = async function () {
  const URL = 'https://www.longyan.cn/bus_wp/busline.json';
  const lineInfo: IallLineInfo = {
    retcode: '001',
    msg: '获取路线信息成功',
    data: [],
  };
  const lineNo = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  await axios
    .all(
      lineNo.map((id) =>
        axios.get(URL, {
          params: {
            searchLineInfo: true,
            lineNo: id,
          },
        }),
      ),
    )
    .then(
      axios.spread(
        (
          { data: line1 },
          { data: line2 },
          { data: line3 },
          { data: line4 },
          { data: line5 },
          { data: line6 },
          { data: line7 },
          { data: line8 },
          { data: line9 },
        ) => {
          lineInfo.data.push(
            line1.data[0],
            line2.data[0],
            line3.data[0],
            line4.data[0],
            line5.data[0],
            line6.data[0],
            line7.data[0],
            line8.data[0],
            line9.data[0],
          );
        },
      ),
    );

  return lineInfo;
};

export { allLineInfo };
